package algorithm.leetcode.I101to200;

import java.util.Arrays;

/**
 * 先排序,然后再遍历
 * O(nlogn)
 */

public class Q128 {

    public int longestConsecutive(int[] nums) {
        if (nums.length == 0) return 0;

        Arrays.sort(nums);

        int currentLenth = 1, maxLength = 1;

        for (int i = 1; i < nums.length; i++) {
            // 递增
            if (nums[i] == nums[i-1]+1) currentLenth ++;
            // 重复
            else if (nums[i] == nums[i-1]) continue;
            // 断开
            else currentLenth = 1;
            maxLength = Math.max(maxLength, currentLenth);
        }

        return maxLength;
    }

}
